
#include "mainWindow.h"
#include "imageLoader.h"
#include "CNetworkManager.h"

#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>

#include <QtGui/QImage>

/*----------------------------------------------------------------------------*/

MainWindow::MainWindow()
	:	m_ui( new Ui::MainWindowUi )	
{
	m_networkManager = new CNetworkManager();
	m_imageLoader = new ImageLoader();
	m_newNetworkWizard = new NewNetworkWizard( *m_networkManager );
	m_trainNetworkWizard = new TrainNetworkWizard( *m_imageLoader, *m_networkManager );

	m_ui->setupUi( this );	

	/** Action: Open */
	connect( m_ui->m_actionOpen, SIGNAL( triggered() ), this, SLOT( DisplayImage() ) );

	/**  Action: Close */
	connect( m_ui->m_actionClose, SIGNAL( triggered() ), this, SLOT( CloseImage() ) );

	/** Action: New network */
	connect( m_ui->m_actionNewNetwork, SIGNAL( triggered() ), this, SLOT( NewNetwork() ) );

	/** Action: Save network */
	connect( m_ui->m_actionSaveNetwork, SIGNAL( triggered() ), this, SLOT( SaveNetwork() ) );

	/** Action: Save As network */
	connect( m_ui->m_actionSaveAsNetwork, SIGNAL( triggered() ), this, SLOT( SaveNetworkAs() ) );

	/** Action: Load network */
	connect( m_ui->m_actionLoadNetwork, SIGNAL( triggered() ), this, SLOT( LoadNetwork() ) );

	/** Action: Close network */
	connect( m_ui->m_actionCloseNetwork, SIGNAL( triggered() ), this, SLOT( CloseNetwork() ) );

	/** Action: Train network */
	connect( m_ui->m_actionTrainNetwork, SIGNAL( triggered() ), this, SLOT( TrainNetwork() ) );

	/** Action: About */
	connect( m_ui->m_actionAbout, SIGNAL( triggered() ), this, SLOT( About() ) );

	/** Action: Quit */
	connect( m_ui->m_actionQuit, SIGNAL( triggered() ), this, SLOT( close() ) );

	/** Actions enabling */
	connect( m_newNetworkWizard, SIGNAL( NetworkAccessible( bool ) ), this, SLOT( NetworkIsAccessible( bool ) ) );

	/** Button: Classify image */
	connect( m_ui->m_classifyButton, SIGNAL( clicked() ), this, SLOT( ClassifyImage() ) );
	
	/** other stuff */
	this->show();	
}


/*----------------------------------------------------------------------------*/


MainWindow::~MainWindow()
{
	if( m_trainNetworkWizard )
		delete m_trainNetworkWizard;
	if( m_newNetworkWizard )
		delete m_newNetworkWizard;
	if( m_imageLoader )
		delete m_imageLoader;
	if ( m_networkManager )
		delete m_networkManager;
	if( m_ui )
		delete m_ui;
}


/*----------------------------------------------------------------------------*/


void MainWindow::TrainNetwork()
{
	if( !m_networkManager->NetworkExists() )
		throw "error";

	m_trainNetworkWizard->ShowCentered( *this );
}


/*----------------------------------------------------------------------------*/


void MainWindow::About()
{
	QMessageBox::about( this, "About", "Designed by:\n - Anastasia Karpinska\n - Stanislav Kudriashev" );
}


/*----------------------------------------------------------------------------*/


void MainWindow::NewNetwork()
{
	if ( m_networkManager->NetworkExists() && !WarningOverwriteNetwork() )			
		return;	

	m_newNetworkWizard->ShowCentered( *this );	
}


/*----------------------------------------------------------------------------*/


void MainWindow::SaveNetwork()
{
	QString fileName;
	if ( m_networkManager->GetNetworkFileName().empty() )
	{
		fileName = GetSaveFileNameDialog();

		if ( !fileName.size() )
			return;		
	}

	m_networkManager->SaveNetwork( fileName.toAscii().data() );

	m_ui->m_actionSaveNetwork->setEnabled( false );
}


/*----------------------------------------------------------------------------*/


void MainWindow::LoadNetwork()
{
	if( m_networkManager->NetworkExists() && !WarningOverwriteNetwork() )			
		return;

	QString fileName = QFileDialog::getOpenFileName(
			this
		,	tr( "Open" )
		,	QDir::current().absolutePath()
		,	tr( "Network (*.neuro);;Any files (*.*)" )		
	);

	if( !fileName.size() )
		return;
	
	m_networkManager->LoadNetwork( fileName.toAscii().data() );

	emit NetworkIsAccessible( true );
}


/*----------------------------------------------------------------------------*/


bool MainWindow::WarningOverwriteNetwork()
{
	int result = QMessageBox::warning( 
		this
		,	"Warning"
		,	"A network is already opened, do you want to overwrite it?"
		,	QMessageBox::Yes
		,	QMessageBox::No
		);
	
	return result == QMessageBox::Yes;
}


/*----------------------------------------------------------------------------*/


void MainWindow::CloseNetwork()
{
	m_networkManager->CloseNetwork();

	emit NetworkIsAccessible( false );
}


/*----------------------------------------------------------------------------*/


void MainWindow::NetworkIsAccessible( bool _state )
{
	m_ui->m_actionSaveNetwork->setEnabled( _state );
	m_ui->m_actionSaveAsNetwork->setEnabled( _state );
	m_ui->m_actionTrainNetwork->setEnabled( _state );
	m_ui->m_actionCloseNetwork->setEnabled( _state );
}


/*----------------------------------------------------------------------------*/


void MainWindow::SaveNetworkAs()
{
	QString fileName = GetSaveFileNameDialog();

	if ( !fileName.size() )
		return;
	
	m_networkManager->SaveNetwork( fileName.toAscii().data() );

	m_ui->m_actionSaveNetwork->setEnabled( false );
}


/*----------------------------------------------------------------------------*/


QString MainWindow::GetSaveFileNameDialog()
{
	return QFileDialog::getSaveFileName(
		this
		,	tr( "Save" )
		,	QDir::current().absolutePath()
		,	tr( "Network (*.neuro)" )
	);
}


/*----------------------------------------------------------------------------*/


void MainWindow::DisplayImage()
{
	QString imageName = QFileDialog::getOpenFileName(
		this
		,	tr( "Open" )
		,	QDir::current().absolutePath()
		,	tr( "Image (*.bmp);;Any files (*.*)" )		
		);

	if( !imageName.size() )
		return;	
	
	m_ui->m_displayWidget->SetImage( new QImage( imageName ) );

}


/*----------------------------------------------------------------------------*/


void MainWindow::CloseImage()
{
	m_ui->m_displayWidget->CloseImage();
}


/*----------------------------------------------------------------------------*/


void MainWindow::ClassifyImage()
{
	CImage* image = m_imageLoader->CreateImageFromBmp( m_ui->m_displayWidget->GetImage() );
	char temp = m_networkManager->ClassifyImage( *image );
	if( image )
		delete image;
	QString text = temp;
	QMessageBox::information( this, "Class", text );
}

/*----------------------------------------------------------------------------*/

